﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using IOP.Extension.DependencyInjection;
using IOP.ISEA;
using System.Linq;
using System.Numerics;

namespace VkSample99
{
    [ModulePriority(ModulePriority.Assets)]
    public class AssetsModule : VulkanModule, IVulkanRenderWapperModule
    {
        public VulkanRenderWapper RenderWapper { get; set; }
        [Autowired]
        private ILogger<AssetsModule> _Logger { get; set; }

        protected override async Task Load(VulkanGraphicsManager graphicsManager)
        {
            try
            {
                var basePath = AppContext.BaseDirectory;
                var modelsPath = Path.Combine(basePath, "Assets", "Models");
                var texPath = Path.Combine(basePath, "Assets", "Textures");
                var lDevice = graphicsManager.VulkanDevice;

                graphicsManager.TryGetRenderGroup("Common", out IRenderGroup group);
                graphicsManager.TryGetRenderGroup("Mirror", out IRenderGroup mirrorGroup);
                ISEADocument document = new ISEADocument();
                await document.Load(Path.Combine(AppContext.BaseDirectory, "test99.isea"));
                var renders = await graphicsManager.CreateRenderObjectsFromISEA<RenderObject>(document);

                var ballArchetype = ContextManager.CreateArchetype("Ball", new TransformComponent(), new BallComponent());
                var mirrorArchetype = ContextManager.CreateArchetype("Mirror", new TransformComponent(), new MirrorComponent());
                var planeArchetype = ContextManager.CreateArchetype(new TransformComponent(), new PlaneComponent());
                var lightArchetype = ContextManager.CreateArchetype(new LightComponent());
                foreach (var item in renders)
                {
                    if(item.Name == "Plane")
                    {
                        ContextManager.CreateContexts(1, planeArchetype, group, item);
                    }
                    else if(item.Name == "Ball")
                    {
                        ContextManager.CreateContexts(1, ballArchetype, group, item);
                        var child = item.GetChildrens().First();
                        ContextManager.CreateContexts(1, mirrorArchetype, mirrorGroup, child);
                    }
                }
                var light = graphicsManager.CreatePointLight<RenderObject>("main", new Vector3(0, 2.5f, 2), new LightStrength(0.4f, 0.4f, 0.4f, 1.0f));
                ContextManager.CreateContexts(1, lightArchetype, group, light);
                ContextManager.CreateContexts(1, lightArchetype, mirrorGroup, light);
            }
            catch (Exception e)
            {
                _Logger.LogError(e.Message + "\r\n" + e.StackTrace);
            }
        }

        protected override Task Unload(VulkanGraphicsManager manager)
        {
            return Task.CompletedTask;
        }
    }
}
